package com.winit.openapi.erp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.erp.spi.common.CityService;
import com.winit.erp.spi.common.DistrictService;
import com.winit.erp.spi.common.RegionService;
import com.winit.erp.spi.common.command.CityCommand;
import com.winit.erp.spi.common.command.DistrictCommand;
import com.winit.erp.spi.common.command.RegionCommand;
import com.winit.erp.spi.common.vo.City;
import com.winit.erp.spi.common.vo.District;
import com.winit.erp.spi.common.vo.RegionVO;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * ERP 基础数据查询 由于UPS的基础数据中，省、城市信息不全，此处需要在ERP-Adapter中获取省和城市信息供用户注册使用 国家使用原UPS的接口
 * 
 * @author long.chen 2015-4-21 下午1:33:53
 * @since 1.0
 */
@Controller
@RequestMapping(value = "/winitBaseData")
public class WinitBaseDateController extends BaseController {

    @Resource
    private RegionService winitRegionService;
    @Resource
    private CityService   winitCityService;
    @Resource
    private DistrictService winitDistrictService;
    @RequestMapping(value = "/getRegionList")
    public String getRegionList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        // 获取参数
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        // 国家ID
        ValidateUtil.valid(json, "countryId", 0, false);
        RegionCommand command = new RegionCommand();
        command.setCountryId(json.getLong("countryId"));
        List<RegionVO> regionList = winitRegionService.query(command);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (RegionVO region : regionList) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("regionId", String.valueOf(region.getCregionId()));
            map.put("regionName", region.getName());
            list.add(map);
        }
        responseMsg.setData(list);
        return SUCCESS;
    }

    @RequestMapping(value = "/getCityList")
    public String getCityList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        // 省份ID
        ValidateUtil.valid(json, "regionId", 0, false);
        CityCommand command = new CityCommand();
        command.setRegionId(json.getLong("regionId"));
        List<City> cityList = winitCityService.getCityById(command);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (City ci : cityList) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("cityId", String.valueOf(ci.getCcityId()));
            map.put("cityName", ci.getName());
            list.add(map);
        }
        responseMsg.setData(list);
        return SUCCESS;
    }
    @RequestMapping(value = "/getDistrictList")
    public String getDistrictList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        // 城市ID
        ValidateUtil.valid(json, "cityId", 0, false);
        DistrictCommand command = new DistrictCommand();
        command.setCityId(json.getInteger("cityId"));
        List<District> districtList = winitDistrictService.queryDistrictsByCityId(command);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (District district : districtList) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("districtId", String.valueOf(district.getDistrictId()));
            map.put("name", district.getName());
            list.add(map);
        }
        responseMsg.setData(list);
        return SUCCESS;
    }

}
